package org.doc.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RuntimeUtil;
import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.doc.config.ChineseFont;
import org.doc.util.MinioUtil;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageSz;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.*;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.concurrent.TimeUnit;

@RestController()
@RequestMapping("/document")
@Slf4j
public class Word2PdfController {

    @Resource
    private MinioUtil minioUtil;

    @GetMapping("/getPdfDoc")
    public String getPdfDoc() {
        // 确保目录存在
        new File("/tmp/doc").mkdirs();
        new File("/tmp/covert").mkdirs();
        //使用autopoi-web创建docx文档
        XWPFDocument document = new XWPFDocument();
        document.createParagraph().createRun().setText("这是一个word文档");
        document.createParagraph().createRun().setText("Hello World");
        document.createParagraph().createRun().setText("Hello World");
        document.createParagraph().createRun().setText("Hello World");
        //转化为pdf必需要设置以下属性不然转化时会报错
        if (document.getStyles() == null) {
            document.createStyles();
        }
        if (document.getDocument().getBody().getSectPr() == null){
            CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
            CTPageSz pageSz = sectPr.addNewPgSz();
            pageSz.setW(BigInteger.valueOf(12240)); //12240 Twips = 12240/20 = 612 pt = 612/72 = 8.5"
            pageSz.setH(BigInteger.valueOf(15840));
        }
        try {
            //创建一个doc文档
            File docxFile = new File("/tmp/doc/这是一个doc文档.doc");
            if (!docxFile.exists()) {
                docxFile.createNewFile();
            }
            FileOutputStream docxOutputStream = new FileOutputStream(docxFile);
            document.write(docxOutputStream);
            docxOutputStream.close();
            //提前创建转化后的pdf文档
            File covertPdfFile = new File("/tmp/covert/这是一个doc文档.pdf");
            if (!covertPdfFile.exists()) {
                covertPdfFile.createNewFile();
            }
            BufferedOutputStream outputStream = FileUtil.getOutputStream(covertPdfFile);
            PdfOptions options = PdfOptions.create();
            options.fontProvider(ChineseFont.CHINESE_FONT_PROVIDER);
            PdfConverter.getInstance().convert(document, outputStream, options);
            //上传到minio
            minioUtil.upload(Files.newInputStream(covertPdfFile.toPath()), "minio上的pdf文档.pdf", "test-bucket");
            if (covertPdfFile.exists()) {
                log.info("删除pdf成功" + covertPdfFile.toPath());
                covertPdfFile.delete();
            }
            if (docxFile.exists()) {
                log.info("删除doc成功" + docxFile.toPath());
                docxFile.delete();
            }
        } catch (Exception e) {
            //打印错误日志
            log.error("转化失败" + e.getMessage());
        }
        return "转化成功";
    }
}
